package com.jams.goodox.activity.serve.report;

import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.goodox.activity.serve.BaseServeActivity;
import com.jams.goodox.activity.serve.ServeSureOrderActivity;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.ReportBean;
import com.jams.goodox.entity.ServeSubmitBean;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

import static com.jams.goodox.utils.Utils.setTextChange;

/**
 * 重构的报事页面
 * Created by guozhen.hou on 2017/7/7.
 */

public class AddReportActivity extends BaseServeActivity implements AdapterView.OnItemClickListener {

    private NoScrollGridView gridType;
    private List<ReportBean> reportBeanList;
    boolean isPersonReport;

    private int oldPosition = -1;//历史选中item
    private ListCommonAdapter listCommonAdapter;
    @Override
    protected String getBarTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected void initData() {
        StyleUtil.titleBackKey(this,getBarTitle());
        isPersonReport = getIntent().getBooleanExtra("isPersonReport", false);
        View view=getLayoutInflater().inflate(R.layout.report_head_view,null);
        addServeHeadView(view);
        gridType= ButterKnife.findById(view,R.id.grid_type);
        initGridVie();
        setEditHint(getResources().getString(R.string.report_add_hint));
        initStopTime();
    }

    /**
     * 初始化类型数据源，适配器以及点击事件
     */
    private void initGridVie(){
        String[] a=getResources().getStringArray(R.array.report_item);
        String[] b = getResources().getStringArray(R.array.report_hint_msg);
        reportBeanList=new ArrayList<>();
        for (int i = 0, len = a.length; i < len; i++) {
            reportBeanList.add(new ReportBean(a[i], b[i], false));
        }
        listCommonAdapter = new ListCommonAdapter<ReportBean>(this, reportBeanList, R.layout.include_tx_hint) {
            @Override
            public void convert(ListCommonViewHolder helper, ReportBean item, int position) {
                TextView tx_hint = helper.getView(R.id.tx_hint);
                tx_hint.setText(item.getReportName());
                setTextChange(tx_hint, item.isChoose());
            }
        };
        gridType.setAdapter(listCommonAdapter);
        gridType.setOnItemClickListener(this);
    }

    @Override
    protected void submitServeOrder() {
        if (oldPosition < 0) {
            ToastUtil.show("请选择一种类型");
            return;
        }
        if (!hasDescription()) {
            ToastUtil.show(this, "请填写事情内容");
            return;
        }
        ServeSubmitBean serveSubmitBean = getSubmitWithBaseParams();
        serveSubmitBean.setType(Constant.SERVE_TYPE_REPORT);
        serveSubmitBean.setItemName(String.format("%s-%s",getBarTitle(),reportBeanList.get(oldPosition).getReportName()));
        serveSubmitBean.setRepairId(String.valueOf(oldPosition + 1));//此处注意，需要传递的是item的位置，由1开始
        serveSubmitBean.setTitle(getIntent().getStringExtra("title"));
        ServeSureOrderActivity.go(this, serveSubmitBean, isPersonReport);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //仅当点击项未被选中进行更换操作：不能点击取消
        if (!reportBeanList.get(position).isChoose()) {
            if (oldPosition >= 0) reportBeanList.get(oldPosition).setChoose(false);
            reportBeanList.get(position).setChoose(true);
            oldPosition = position;
            listCommonAdapter.notifyDataSetChanged();

            setEditHint(reportBeanList.get(position).getReportHintMsg());
        }
    }
}
